<script lang="ts">
import {defineComponent, PropType, computed, onBeforeMount, ref} from "vue"
import {TableColumn} from "@/components/AppTable/TableType";
import {SlotsType} from "vue";
import request from "@/units/request";
import {useCloseMessageBox} from "@/hooks/DialogMsg";
import {CloneData} from "@/units/tool";

export default defineComponent({
  name: "DataPanel",
  slots: Object as SlotsType<{ where: {} }>,
  emits: {
    "choice": (value: any | any[]) => true,
    "update-choice": (value: Array<{ value: string | number, label: string }>) => true,
  },
  props: {
    title: {
      type: String,
      default: '数据搜索'
    },
    api: {//请求地址
      type: String,
      required: true
    },
    query: {//添加默认搜索条件
      type: Object as PropType<{ [key: string]: any }>,
      default() {
        return {}
      }
    },
    columns: {//表单配置列
      type: Array as PropType<Array<TableColumn>>,
      required: true
    },
    multiple: {//是否是什么值 true多选  false单选
      type: Boolean,
      default: false
    },
    choice: {
      type: Object as PropType<Array<{ value: string | number, label: string }>>,
      default() {
        return []
      }
    }
  },
  setup(props, {slots, emit}) {
    const ChoiceData = ref(props.choice)
    /**
     * 搜索数据
     */
    const list = ref([])

    /**
     * 分页数据
     */
    const pagination = ref({
      page: 1,
      size: 15,
      total: 0,
    })

    /**
     * 搜索条件
     */
    const where = ref({})

    /**
     * 插槽key
     */
    const slotsKey = Object.keys(slots)


    /**
     * 多选值
     */
    const MultipleValue = ref([])


    /**
     * 关闭窗口
     */
    const close = useCloseMessageBox()

    /**
     * 处理搜索列数据
     */
    const TableColumns = computed(() => {
      const columns = CloneData(props.columns)
      if (props.multiple) {//多选
        columns.unshift({type: "selection", width: 40, fixed: "left"})
        return columns
      } else {//单选
        return columns.concat([
          {type: "action", label: "操作", width: "75", fixed: "right"}
        ])
      }
    })

    /**
     * 选择数据
     * @constructor
     */
    function SelectData(value: any | any[]) {
      emit('choice', value)
      close()
    }

    /**
     * 多选
     * @constructor
     */
    function MultipleChoice(ids: Array<string | number>, raw: Array<any>) {
      MultipleValue.value = raw
    }

    /**
     * 分页切换
     * @param page_num
     * @param size_num
     * @constructor
     */
    function PaginationChange(page_num: number, size_num: number) {
      pagination.value.page = page_num
      pagination.value.size = size_num
      getData()
    }

    /**
     * 删除标签
     */
    function closeTags(index: number) {
      ChoiceData.value.splice(index, 1)
      emit('update-choice', ChoiceData.value)
    }

    /**
     * 获取数据
     */
    async function getData() {
      const {code, data} = await request(props.api, {
        page: pagination.value.page,
        size: pagination.value.size,
        ...props.query,
        ...where.value
      })
      if (code === 200) {
        pagination.value = {
          page: data.current,
          size: data.size,
          total: data.total
        }
        list.value = data.list
      }
    }


    getData()


    function search() {
      pagination.value.page = 1
      getData()
    }

    return {
      list,
      pagination,
      slotsKey,
      TableColumns,
      SelectData,
      where,
      MultipleChoice,
      MultipleValue,
      PaginationChange,
      close,
      ChoiceData,
      closeTags,
      search
    }
  }
})
</script>

<template>
  <div>
    <el-page-header>
      <template #content>
        {{ title }}
        <el-popover v-if="ChoiceData.length>0" placement="bottom-start" title="当前已选" width="350px" trigger="hover">
          <template #reference>
            <el-link type="primary" :underline="false">查看已选({{ ChoiceData.length }})</el-link>
          </template>
          <template #default>
            <el-tag closable class="app-margin-left-small app-margin-bottom-small" v-for="(item,index) in ChoiceData" :key="index" @close="closeTags(index)" type="primary">
              {{ item.label }}
            </el-tag>
          </template>
        </el-popover>
      </template>
      <template #extra>
        <el-space>
          <el-button v-if="MultipleValue.length>0 && multiple" @click="SelectData(MultipleValue)" type="primary">
            选择
          </el-button>
          <el-button type="danger" @click="close">
            关闭
          </el-button>
        </el-space>
      </template>
    </el-page-header>
    <data-card shadow="never" class="app-margin-top-interval">
      <template v-if="slotsKey.indexOf('where')>-1" #left>
        <div class="app-margin-bottom-interval">
          <slot :where="where" :search="search" name="where"></slot>
        </div>
      </template>
      <app-table height="440px" @choice="MultipleChoice" :columns="TableColumns" :data="list">
        <template v-if="!multiple" #action="{ row }">
          <el-button @click="SelectData(row)" type="primary" size="small">
            选择
          </el-button>
        </template>
      </app-table>
      <app-pagination @change="PaginationChange" class="app-margin-top-interval" layout="prev,pager,next,total" v-bind="pagination"></app-pagination>
    </data-card>
  </div>
</template>
<style scoped lang="scss">

</style>